package cn.tedu.supermarket.user.service.impl;

import cn.tedu.supermarket.base.exception.ServiceException;
import cn.tedu.supermarket.base.response.StatusCode;
import cn.tedu.supermarket.user.mapper.UserMapper;
import cn.tedu.supermarket.user.pojo.dto.UserLoginParam;
import cn.tedu.supermarket.user.pojo.dto.UserQuery;
import cn.tedu.supermarket.user.pojo.vo.UserVO;
import cn.tedu.supermarket.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 业务层实现类需要添加@Service注解,表示其为业务层实现类
 */
@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public UserVO login(UserLoginParam userLoginParam) {
        log.debug("用户登录参数：{}", userLoginParam);
        UserVO userVO = userMapper.selectByUsername(userLoginParam.getUsername());
        // 用户名不存在
        if (userVO == null) {
            //如果主业务遇到了问题,需要抛出业务层异常,此时的异常结果由全局异常处理器返回给前端
            throw new ServiceException(StatusCode.USERNAME_ERROR);
        }
        // 密码错误
        if (!userVO.getPassword().equals(userLoginParam.getPassword())) {
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        }
        //只有不出现上方的两个异常情况,才会执行下面的代码,将业务层结果交给controller返回给前端
        log.debug("用户查询结果：{}", userVO);
        return userVO;
    }

    @Override
    public List<UserVO> selectUser(UserQuery userQuery) {
        log.debug("查询用户信息：{}", userQuery);
        List<UserVO> list = userMapper.selectUser(userQuery);
        return list;
    }
}